package tech.aistar.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import tech.aistar.entity.User;
import tech.aistar.service.IUserService;

import java.util.List;

@Controller
public class UserController {

    @Autowired //表示当前类依赖于 IUserService， 并且对象从Spring容器中通过类型匹配获得
    private IUserService userService;
    //根据主键查询
    //url: http://localhost:8080:/springboot_demo/userjson/1

    //查询所有
    @GetMapping("user")
    public ModelAndView getAll(){
        List<User> userList = userService.list();
        ModelAndView mav = new ModelAndView();
        mav.addObject("userList",userList);
        mav.setViewName("user/list");
        return mav;
    }

    @GetMapping("user/getById")
    public ModelAndView getById(@RequestParam("userId") Integer userId){
        User user = userService.getById(userId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("user",user);
        mav.setViewName("user/detail");
        return mav;
    }

    @GetMapping("user/getByName")
    public ModelAndView getByName(@RequestParam("userName") String userName){
        User user = userService.getByName(userName);
        ModelAndView mav = new ModelAndView();
        mav.addObject("user",user);
        mav.setViewName("user/detail");
        return mav;
    }

    @GetMapping("user/getByLikeName")
    public ModelAndView getByLikeName(@RequestParam("userLikeName") String userLikeName){
        List<User> userList = userService.getLikeUser(userLikeName);
        ModelAndView mav = new ModelAndView();
        mav.addObject("userList",userList);
        mav.setViewName("user/list");
        return mav;
    }

    @GetMapping("user/page/{pagenum}")
    public ModelAndView getByPage(@PathVariable Integer pagenum){
        Page<User> pageInfo =userService.getByPage(pagenum);
        ModelAndView mav=new ModelAndView();
        mav.addObject("pageInfo",pageInfo);
        mav.setViewName("user/all");
        return mav;
    }

    @PostMapping("user/save")
    public ModelAndView save(User user){
        ModelAndView mav=new ModelAndView();
        boolean result=userService.save(user);
        if(result){
            mav.addObject("msg","用户添加成功！");
            mav.setViewName("user/success");
        }
        else{
            mav.addObject("msg","用户添加失败！");
            mav.setViewName("user/success");
        }
        return mav;
    }

    //删除用户
    @DeleteMapping("user")
    @ResponseBody
    public Boolean delete(@RequestParam("userId")Integer userId){

        boolean result = userService.removeById(userId);
        System.out.println("删除后:" + userId + ", result:" + result);
        return result;

    }


    //修改用户信息:1. 查询用户信息到修改页面
    @GetMapping("user/{userId}")
    public ModelAndView getByIdForUpdate(@PathVariable("userId") Integer userId){
        ModelAndView mav = new ModelAndView();
        User user = userService.getById(userId);
        mav.addObject("user",user);
        mav.setViewName("user/update");
        return mav;
    }


    //修改用户信息:  2. 修改用户信息
    @PutMapping("user")
    @ResponseBody
    public boolean update(User user){
        boolean result  =userService.updateById(user);
        return result;
    }
}
